<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 配置中心->配置管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-row :gutter="10">
                    <el-col :xs="24" :sm="12" :lg="6">
                        <el-input v-model="query.data.configKey" placeholder="配置Key" class="handle-input"></el-input>
                    </el-col>
                    <el-col :xs="24" :sm="12" :lg="6">
                        <el-input v-model="query.data.group" placeholder="配置Group" class="handle-input"></el-input>
                    </el-col>
                    <el-col :xs="24" :sm="12" :lg="6">
                        <el-input v-model="query.data.namespace" placeholder="命名空间" class="handle-input"></el-input>
                    </el-col>
                    <el-col :xs="24" :sm="12" :lg="6">
                        <el-select v-model="query.data.appCode" placeholder="所属应用" class="handle-select" clearable>
                            <el-option v-for="item in appList"
                                       :key="item.appCode"
                                       :label="item.appName"
                                       :value="item.appCode">
                                <span style="float: left">{{ item.appName }}</span>
                            </el-option>
                        </el-select>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="24" :sm="12" style="text-align: left">
                        <el-button type="primary" icon="el-icon-folder-add" @click="handleAdd">新增</el-button>
                    </el-col>
                    <el-col :xs="24" :sm="12" style="text-align: right">
                        <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
                        <el-button type="primary" icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
                    </el-col>
                </el-row>
            </div>

            <el-table :data="tableData"
                      border
                      class="table"
                      ref="multipleTable"
                      header-cell-class-name="table-header"
                      @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="40" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="60"></el-table-column>
                <el-table-column prop="configKey" label="配置Key" width="180"></el-table-column>
                <el-table-column prop="content" label="配置内容"></el-table-column>
                <el-table-column prop="group" label="配置Group"></el-table-column>
                <el-table-column prop="namespace" label="命名空间"></el-table-column>
                <el-table-column prop="appCode" label="所属应用"></el-table-column>
                <el-table-column prop="remark" label="备注"></el-table-column>
                <el-table-column prop="status" label="状态"></el-table-column>
                <el-table-column prop="date" label="操作信息"></el-table-column>
                <el-table-column label="操作" width="130" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit"
                                   @click="handleEdit(scope.$index, scope.row)">
                            编辑
                        </el-button>
                        <el-button type="text" icon="el-icon-delete" class="red"
                                @click="handleDelete(scope.$index, scope.row)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageVo.pageNo"
                        :page-size="query.pageVo.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="50%" max-height="480px">
            <el-form ref="form" :model="formData" label-width="100px">
                <el-row :gutter="10">
                    <el-col :md="24">
                        <el-form-item label="ID" v-show="formData.id ? true:false">
                            <el-input v-model="formData.id" :disabled="formData.id ? 'disabled':false"></el-input>
                        </el-form-item>
                        <el-form-item label="配置Key">
                            <el-input v-model="formData.configKey" :disabled="formData.id ? 'disabled':false"></el-input>
                        </el-form-item>
                        <el-form-item label="配置Group">
                            <el-input v-model="formData.group"></el-input>
                        </el-form-item>
                        <el-form-item label="命名空间">
                            <el-input v-model="formData.namespace"></el-input>
                        </el-form-item>
                        <el-form-item label="所属应用">
                            <el-select v-model="formData.appCode" class="handle-select">
                                <el-option v-for="item in appList"
                                                   :label="item.appName"
                                                   :value="item.appCode">
                                    <span style="float: left">{{ item.appName }}</span>
                                    <span style="float: right">{{ item.appCode }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :md="12">
                        <el-form-item label="数据格式">
                            <el-radio-group v-model="formData.mode">
                                <el-radio-button v-for="item in modeList" :label="item.value">
                                    {{ item.label }}
                                </el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :md="12">
                        <el-form-item label="配置状态">
                            <el-radio-group v-model="formData.status">
                                <el-radio-button v-for="item in statusList" :label="item.value">
                                    {{ item.label }}
                                </el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :md="24">
                        <el-form-item label="配置内容">
                            <el-input type="textarea" v-model="formData.content"></el-input>
                        </el-form-item>
                        <el-form-item label="备注">
                            <el-input type="textarea" v-model="formData.remark"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="saveEdit">确 定</el-button>
                <el-button @click="editVisible = false">取 消</el-button>
            </span>
        </el-dialog>

    </div>

</template>

<script>
    import { doFindConfig, doGetConfig, doSaveConfig, doDeleteConfig } from '../../api/configic/appConfig';
    import { fetchDictByIndex } from '../../api/resic/dictionary';
    import { OnionUtils } from '../../utils/onion-utils';
    import { listApp } from '../../api/appic/appInfo';

    export default {
        name: 'basetable',
        data() {
            return {
                query: {
                    data:{
                        configKey: '',
                        group: '',
                        namespace: '',
                        appCode: ''
                    },
                    pageVo:{
                        pageNo: 1,
                        pageSize: 10
                    }
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                pageTotal: 0,
                formData: {},
                idx: -1,
                id: -1,
                appList:[],
                statusList:[{"value":"enabled", "label":"启用"}, {"value":"disabled", "label":"停用"}],
                modeList:[{"value":"text", "label":"文本"},{"value":"JSON", "label":"json"},{"value":"XML", "label":"xml"}]
            };
        },
        created() {
            this.initData();
            this.getData();
        },
        //方法区
        methods: {
            initData(){
                let appQueryParam = {
                    param: {},
                    pageVo: { pageNo: 1, pageSize: 100 }
                };
                listApp(appQueryParam).then(result => {
                    this.appList = result.data;
                });
            },
            //重置查询参数
            resetQueryForm(){
                this.query.data = {};
            },
            //获取应用信息列表
            getData() {
                doFindConfig(this.query).then(res => {
                    this.tableData = res.data.rows;
                    this.pageTotal = res.data.total || 50;
                });
            },
            //触发查询按键
            handleQuery() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            delAllSelection() {
                const length = this.multipleSelection.length;
                let str = '';
                this.delList = this.delList.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + ' ';
                }
                this.$message.error(`删除了${str}`);
                this.multipleSelection = [];
            },
            // 新增操作
            handleAdd(){
                this.formData = {
                    mode : "text",
                    status : "enabled"
                };
                this.editVisible = true;
            },
            // 编辑操作
            handleEdit(index, row) {
                this.idx = index;
                this.formData = row;
                this.editVisible = true;
            },
            // 保存编辑
            saveEdit() {
                doSaveConfig(this.formData).then(result => {
                    this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                    this.$set(this.tableData, this.idx, result.data);
                    this.editVisible = false;
                });
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                .then(() => {
                    doDeleteConfig(this.tableData[index].id).then(result =>{
                        this.$message.success('删除成功');
                        this.tableData.splice(index, 1);
                    });
                })
                .catch(() => {
                    this.$alert("异常！")
                });
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            }
        }
    };
</script>

<style>
    .handle-box {
        margin-bottom: 10px;
    }

    .handle-select {
        width: 100%;
    }

    .handle-input {
        width: 100%;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }

    .el-dialog__body{
        padding: 15px;
    }

    ul.ztree {
        width: auto;
        height: 410px;
        overflow-y: auto;
    }

    div.zTreeDemoBackground{
        height: 300px;
    }

    div#zTreeMenu {
        z-index: 9999;
        position:fixed;
        visibility:hidden;
        top:0;
        background-color: #555;
        text-align: left;
        padding: 2px;
    }

    div#zTreeMenu ul li{
        margin: 1px 0;
        padding: 0 5px;
        cursor: pointer;
        list-style: none outside none;
        background-color: #DFDFDF;
    }

</style>